<template>
  <div class="contain-login">
    <el-card>
      <!-- logo图片 -->
      <img src="../../assets/logo_index.png" alt />
      <!-- 表单项 -->
      <el-form :model="loginForm" :rules="loginRules" ref="form">
        <!-- 第一行  请输入手机号码 -->
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="loginForm.mobile"></el-input>
        </el-form-item>
        <!-- 第二行 请输入验证码 -->
        <el-form-item prop="code">
          <el-input
            placeholder="请输入验证码"
            style="width:240px;margin-right:8px"
            v-model="loginForm.code"
          ></el-input>
          <el-button plain>发送验证码</el-button>
        </el-form-item>
        <!-- 第三行 复选框  确认协议 -->
        <el-form-item>
          <el-checkbox :value="true">我已阅读并同意用户协议和隐私条款</el-checkbox>
        </el-form-item>
        <!-- 第四行 登录按钮 -->
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="submitForm('form')">登 录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import Auth from "@/utils/auth";
export default {
  name: "page-login",
  data() {
    let checkNumber = (rule, value, callback) => {
      if (/^1[3-9]\d{9}$/.test(value)) {
        callback();
      } else {
        callback(new Error("手机号码格式不正确"));
      }
    };
    return {
      loginForm: {
        mobile: "",
        code: ""
      },
      loginRules: {
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          // 自定义验证
          { validator: checkNumber, trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { len: 6, message: "验证码是6个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // this.$http
          //   .post(
          //     "http://ttapi.research.itcast.cn/mp/v1_0/authorizations",
          //     this.loginForm
          //   )
          //   .then(res => {
          //     Auth.setUser(res.data.data);
          //     this.$router.push("/");
          //   })
          //   .catch(e => {
          //     this.$message.error("手机号或者验证码错误");
          //   });
          //使用async和await来操作;
          try {
            const res = await this.$http.post("authorizations", this.loginForm);
            Auth.setUser(res.data.data);
            this.$router.push("/");
          } catch (e) {
            this.$message.error("手机号或者验证码错误");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.contain-login {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: url(../../assets/login_bg.jpg) no-repeat center / cover;
}
.el-card {
  width: 400px;
  height: 350px;
  position: relative;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  img {
    display: block;
    margin: 0 auto 20px;
    width: 200px;
  }
}
</style>